<template>
	<view class="addgroup">
		<u-navbar left-icon-color="#fff" :placeholder="true" :bgColor="bgColor" :autoBack="true">
			<view class="navtitle" slot="center">
				{{title}}
			</view>
		</u-navbar>
		<view class="search">
			<u--input placeholder="请输入对方账号或备注" customStyle="background-color: #232526; padding: 20rpx;"
				prefixIconStyle="color: #fff;" @confirm="gosearch" @input="input" prefixIcon="search" color="#fff"
				border="none" v-model="value"></u--input>
		</view>
		<view class="tabs" v-if="isshow == true">
			<u-tabs :list="tabslist" :current="current" lineColor="#fff" activeStyle="color:#fff"
				inactiveStyle="color:#d5d5d5" @click="click"></u-tabs>
		</view>
		<view class="index-list">
			<view v-if="list.length > 0">
				<checkbox-group @change="checkboxChange">
					<label class="uni-list-cell uni-list-cell-pd" v-for="(item,index) in list" :key="item.value">
						<view class="anchor-list">
							<view class="anchor-left">
								<!-- 使用 u-checkbox 组件作为复选框 -->
								<checkbox :value="current == 0? JSON.stringify(item.id):JSON.stringify(item.member_id) "
									color="#17191A" :checked="item.show" />
							</view>
							<view class="anchor-right">
								<!-- 在 u-image 中展示用户头像 -->
								<u-image :showLoading="true" radius="10" :src="item.u_img"
									loadingIcon="/static/chat/logo.png" errorIcon="/static/chat/logo.png" width="50px"
									height="50px">
								</u-image>
								<!-- 根据条件显示用户名或备注 -->
								<view class="text">{{ item.user }}</view>
							</view>
						</view>
					</label>
				</checkbox-group>
			</view>
			<view v-if="list.length < 1">
				<u-empty mode="list" text="成员列表为空" :marginTop="100" icon="http://cdn.uviewui.com/uview/empty/list.png">
				</u-empty>
			</view>
		</view>
		<view style="height: 100rpx;"></view>
		<view class="flex-btn">
			<view class="left-box">
				<u-button type="primary" v-if="checkall == false" @click="goall" style="color: #17191A;"
					color="linear-gradient(90deg, #C8F673 0%, #98F083 100%)" text="全选"></u-button>
				<u-button type="primary" v-else @click="gocellall" style="color: #17191A;"
					color="linear-gradient(90deg, #C8F673 0%, #98F083 100%)" text="取消全选"></u-button>
			</view>
			<view class="right-box">
				<u-button type="error" v-if="current == 0" @click="gospeech(1)" text="踢出群聊"></u-button>
				<u-button type="primary" v-if="current == 1" @click="gospeech(2)" text="添加成员"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: '',
				bgColor: 'rgba(0,0,0,0.0)',
				value: '',
				list: [],
				selectedItems: [],
				checkall: false,
				current: 0,
				userinfo: {},
				tabslist: [{
						name: '成员管理'
					},
					{
						name: '添加成员'
					}
				],
				checklist: [],
				hisinfo: {},
				isshow: true,
				title:'成员管理'
			}
		},
		onLoad(e) {
			this.userinfo = uni.getStorageSync('info');
			this.id = e.id
			if (e.ishman == 1) {
				this.userlist()
				this.posthisinfo()
				this.title = '成员管理'
			} else {
				this.current = 1
				this.newlist()
				this.posthisinfo()
				this.isshow = false
				this.title = '邀请好友'
			}
		},
		methods: {
			posthisinfo() {
				this.$Request({
					method: 'POST',
					url: '/api/rong/getGroupInfo',
					data: {
						group_id: this.id,
					}
				}).then(res => {
					if (res.code == 1) {
						this.hisinfo = res.group_info
					}
				})
			},
			gosearch() {
				if (this.value == '') {
					uni.showToast({
						title: '请输入对方账号！',
						icon: 'none'
					})
					return
				}
				let searchlist = this.list.flat().filter(item =>
					item.user.includes(this.value)
				);
				this.list = searchlist
			},
			input(e) {
				if (e == '') {
					if (this.current == 0) {
						this.userlist()
					} else {
						this.newlist()
					}
				}
			},
			click(e) {
				this.value = ''
				this.checkall = false
				this.current = e.index
				this.list = []
				if (e.index == 0) {
					this.userlist()
				} else {
					this.newlist()
				}
			},
			goall() {
				if (this.list.length < 1) {
					uni.showToast({
						title: '成员为空！',
						icon: 'none'
					})
					return
				}
				if (this.current == 0) {
					this.list.map(item => {
						item.show = true
						this.selectedItems.push(item.id)
					})
				} else {
					this.list.map(item => {
						item.show = true
						this.selectedItems.push(item.member_id)
					})
				}

				this.checkall = true
			},
			gocellall() {
				if (this.list.length < 1) {
					uni.showToast({
						title: '成员为空！',
						icon: 'none'
					})
					return
				}
				this.list.map(item => {
					item.show = false
					this.selectedItems = []
				})
				this.checkall = false
			},
			gospeech(type) {
				if (this.selectedItems.length < 1) {
					uni.showToast({
						title: '请选择成员！',
						icon: 'none'
					})
					return
				}
				if (type == 1) {
					this.$Request({
						method: 'POST',
						url: '/api/rong/rejectGroup',
						data: {
							group_id: this.id,
							member_id: this.selectedItems.toString(),
						}
					}).then(res => {
						if (res.code == 1) {
							uni.showToast({
								title: '成员已被踢出群聊！',
								icon: 'none'
							})
							this.userlist()
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
				} else {
					this.$Request({
						method: 'POST',
						url: '/api/rong/addGroupUser',
						data: {
							group_id: this.id,
							member_ids: this.selectedItems.toString(),
						}
					}).then(res => {
						if (res.code == 1) {
							uni.showToast({
								title: '添加成功！',
								icon: 'none'
							})
							const PersonMessage = this.$RongIMLib.registerMessageType(
								'RC:adduser',
								true, true, [],
								false)
							console.log(this.checklist)
							this.selectedItems.map(item => {
								console.log(item)
								this.list.map(i => {
									if (item == i.member_id) {
										console.log(1)
										const message = new PersonMessage({
											addname: i.user,
											receiveName: this.hisinfo.group_name,
											receivePortraitUri: this.hisinfo.group_img,
										})
										this.$RongIMLib.sendMessage({
											conversationType: this.$RongIMLib
												.ConversationType.GROUP,
											targetId: this.id
										}, message).then(res1 => {
											if (res1.code == 0) {
												console.log(res1.code)
											} else {
												console.log(res1.code)
											}
										})
									}
								})
							})
							this.newlist()
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					})
				}
			},
			checkboxChange(e) {
				this.selectedItems = e.detail.value
				console.log(this.selectedItems)
			},
			gochat(item) {
				let name = item.break
				if (item.break == '') {
					name = item.user
				}
				uni.navigateTo({
					url: '/pages/chat/chitchat/index?id=' + item.member_id + '&name=' + name
				})
			},
			gourl(url) {
				uni.navigateTo({
					url: url
				})
			},
			userspeechlist() {
				this.$Request({
					method: 'POST',
					url: '/api/rong/userFriendsList',
					data: {
						use_id: this.userinfo.member_id
					}
				}).then(res => {
					if (res.code == 1) {
						let list = res.data;
						let arr = [];
						// 将数据合并到 arr 中
						for (let i in list) {
							list[i].map(item => {
								item.show = false;
								arr.push(item);
							});
						}
						// 使用 filter 过滤掉满足条件的元素
						arr = arr.filter(item => {
							// 判断 item.member_id 是否在 this.checklist 中存在
							return !this.checklist.some(i => item.member_id === i.id);
						});
						this.list = arr

					}
				})
			},
			newlist() {
				this.$Request({
					method: 'POST',
					url: '/api/rong/getGroupInfo',
					data: {
						group_id: this.id
					}
				}).then(res => {
					if (res.code == 1) {
						let list = res.data
						if (list.length < 1) {
							this.checklist = []
						} else {
							let arr = []
							list.map(item => {
								if (item.is_hman != 1) {
									item.show = false
									arr.push(item)
								}
							})
							this.checklist = arr
							this.userspeechlist()
						}
					}
				})
			},
			userlist() {
				this.$Request({
					method: 'POST',
					url: '/api/rong/getGroupInfo',
					data: {
						group_id: this.id
					}
				}).then(res => {
					if (res.code == 1) {
						let list = res.data
						if (list.length < 1) {
							this.list = []
						} else {
							let arr = []
							list.map(item => {
								if (item.is_hman != 1) {
									item.show = false
									arr.push(item)
								}
							})
							this.list = arr
						}
					}
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #17191A;
	}
</style>
<style scoped lang="scss">
	.addgroup {
		.search {
			padding: 30rpx 30rpx 20rpx 30rpx;
		}

		.tabs {
			width: 300rpx;
			margin: 0 auto;
			display: flex;
			justify-content: center;
		}

		.flex-btn {
			width: 100%;
			padding: 30rpx;
			position: fixed;
			bottom: 0;
			left: 0;
			background-color: #1D2123;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.left-box {
				width: 30%;
			}

			.right-box {
				width: 60%;
			}
		}

		.navtitle {
			color: #fff;
		}

		.index-list {
			padding: 30rpx;

			.anchor-list {
				width: 100vw;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 30rpx;

				.anchor-left {
					width: 8%;
				}

				.anchor-right {
					width: 90%;
					display: flex;
					align-items: center;

					.text {
						color: #fff;
						font-size: 28rpx;
						margin-left: 20rpx;
					}
				}
			}
		}
	}
</style>